package org.limkokwing.hami.ui.form;

import com.jappit.midmaps.googlemaps.GoogleMaps;
import com.jappit.midmaps.googlemaps.GoogleMapsCoordinates;
import com.jappit.midmaps.googlemaps.GoogleStaticMap;
import com.jappit.midmaps.googlemaps.GoogleStaticMapHandler;
import com.sun.lwuit.Command;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import org.limkokwing.hami.ui.map.MapProvider;

/**
 * @author Mohammad
 */
public class MapFormHandler extends FormHandler implements GoogleStaticMapHandler, ActionListener {

    private int zoom = 8;
    private double lat = -7.777067;
    private double lng = 110.41798;
    private GoogleMaps gMaps = null;
    private GoogleStaticMap map = null;
    private Form f;
    private Label canvas;
    private MapProvider provider;

    public MapFormHandler() {
        this.provider = new MapProvider();
    }

    public String getName() {
        return "Map";
    }

    public String getTitle() {
        return "Map";
    }

    public void cleanup() {
    }

    protected void execute(final Form f) {
        this.f = f;

        f.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        f.setScrollable(false);
        canvas = new Label();
        f.addComponent(canvas);


        gMaps = new GoogleMaps();

        map = gMaps.createMap(f.getWidth(), f.getHeight(), GoogleStaticMap.FORMAT_PNG);

        map.setHandler(this);

        map.setCenter(new GoogleMapsCoordinates(2.909057, 101.662919));

        map.setZoom(15);

        map.update();

        f.addCommand(new Command("Zoom in") {
          public void actionPerformed(ActionEvent evt) {
                map.zoomIn();
            }

        });
        f.addCommand(new Command("Zoom out") {

            public void actionPerformed(ActionEvent evt) {
                map.zoomOut();
            }

        });
        f.addCommand(new Command("My Location") {

            public void actionPerformed(ActionEvent evt) {
            }

        });
        f.addCommand(new Command("SMS My Location") {

            public void actionPerformed(ActionEvent evt) {
            }

        });

        f.addGameKeyListener(Display.GAME_DOWN, this);
        f.addGameKeyListener(Display.GAME_UP, this);
        f.addGameKeyListener(Display.GAME_LEFT, this);
        f.addGameKeyListener(Display.GAME_RIGHT, this);
    }

    /**
     * Returns the text that should appear in the help command
     */
    public String getHelp() {
        return "Demonstrates theme switching, select a theme to see the changes occur in the application"
                + " A Theme can modify colors, backgrounds, fonts, transparency, margin and padding.";
    }

    public void GoogleStaticMapUpdateError(GoogleStaticMap map, int errorCode, String errorMessage) {
        showError("map error: " + errorCode + ", " + errorMessage);
    }

    public void GoogleStaticMapUpdated(GoogleStaticMap map) {
        try {
            javax.microedition.lcdui.Image img = map.getImage();
            int[] rgb = new int[img.getHeight() * img.getWidth()];
            map.getImage().getRGB(rgb, 0, img.getWidth(), 0, 0, img.getWidth(), img.getHeight() );
            canvas.setIcon(Image.createImage(rgb, img.getWidth(), img.getHeight()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void actionPerformed(ActionEvent ae) {
        int key = ae.getKeyEvent();
        System.out.println("KEY : " + key);
        map.move(key);
    }


    void showError(String message) {
//        Alert error = new Alert("Error", message, null, AlertType.ERROR);
//
//        Display.getDisplay(midlet).setCurrent(error, this);
    }
}
